home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / include / apache / ap_alloc.h next >
Encoding:
C/C++ Source or Header  |  2001-03-06  |  16.0 KB  |  409 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * Portions of this software are based upon public domain software
  55.  * originally written at the National Center for Supercomputing Applications,
  56.  * University of Illinois, Urbana-Champaign.
  57.  */
  58.  
  59. #ifndef APACHE_ALLOC_H
  60. #define APACHE_ALLOC_H
  61.  
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65.  
  66. /*
  67.  * Resource allocation routines...
  68.  *
  69.  * designed so that we don't have to keep track of EVERYTHING so that
  70.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  71.  * particularly in the presence of die()).
  72.  *
  73.  * Instead, we maintain pools, and allocate items (both memory and I/O
  74.  * handlers) from the pools --- currently there are two, one for per
  75.  * transaction info, and one for config info.  When a transaction is over,
  76.  * we can delete everything in the per-transaction pool without fear, and
  77.  * without thinking too hard about it either.
  78.  *
  79.  * rst
  80.  */
  81.  
  82. /* Arenas for configuration info and transaction info
  83.  * --- actual layout of the pool structure is private to 
  84.  * alloc.c.  
  85.  */
  86.  
  87.  /* Need declaration of DIR on Win32 */
  88. #ifdef WIN32
  89. #include "readdir.h"
  90. #endif
  91.  
  92. typedef struct pool pool;
  93. typedef struct pool ap_pool;
  94.  
  95. pool * ap_init_alloc(void);        /* Set up everything */
  96. void ap_cleanup_alloc(void);
  97. API_EXPORT(pool *) ap_make_sub_pool(pool *);    /* All pools are subpools of permanent_pool */
  98. API_EXPORT(void) ap_destroy_pool(pool *);
  99.  
  100. /* pools have nested lifetimes -- sub_pools are destroyed when the
  101.  * parent pool is cleared.  We allow certain liberties with operations
  102.  * on things such as tables (and on other structures in a more general
  103.  * sense) where we allow the caller to insert values into a table which
  104.  * were not allocated from the table's pool.  The table's data will
  105.  * remain valid as long as all the pools from which its values are
  106.  * allocated remain valid.
  107.  *
  108.  * For example, if B is a sub pool of A, and you build a table T in
  109.  * pool B, then it's safe to insert data allocated in A or B into T
  110.  * (because B lives at most as long as A does, and T is destroyed when
  111.  * B is cleared/destroyed).  On the other hand, if S is a table in
  112.  * pool A, it is safe to insert data allocated in A into S, but it
  113.  * is *not safe* to insert data allocated from B into S... because
  114.  * B can be cleared/destroyed before A is (which would leave dangling
  115.  * pointers in T's data structures).
  116.  *
  117.  * In general we say that it is safe to insert data into a table T
  118.  * if the data is allocated in any ancestor of T's pool.  This is the
  119.  * basis on which the POOL_DEBUG code works -- it tests these ancestor
  120.  * relationships for all data inserted into tables.  POOL_DEBUG also
  121.  * provides tools (ap_find_pool, and ap_pool_is_ancestor) for other
  122.  * folks to implement similar restrictions for their own data
  123.  * structures.
  124.  *
  125.  * However, sometimes this ancestor requirement is inconvenient --
  126.  * sometimes we're forced to create a sub pool (such as through
  127.  * ap_sub_req_lookup_uri), and the sub pool is guaranteed to have
  128.  * the same lifetime as the parent pool.  This is a guarantee implemented
  129.  * by the *caller*, not by the pool code.  That is, the caller guarantees
  130.  * they won't destroy the sub pool individually prior to destroying the
  131.  * parent pool.
  132.  *
  133.  * In this case the caller must call ap_pool_join() to indicate this
  134.  * guarantee to the POOL_DEBUG code.  There are a few examples spread
  135.  * through the standard modules.
  136.  */
  137. #ifndef POOL_DEBUG
  138. #ifdef ap_pool_join
  139. #undef ap_pool_join
  140. #endif
  141. #define ap_pool_join(a,b)
  142. #else
  143. API_EXPORT(void) ap_pool_join(pool *p, pool *sub);
  144. API_EXPORT(pool *) ap_find_pool(const void *ts);
  145. API_EXPORT(int) ap_pool_is_ancestor(pool *a, pool *b);
  146. #endif
  147.  
  148. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  149.  
  150. API_EXPORT(void) ap_clear_pool(struct pool *);
  151.  
  152. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  153.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  154.  */
  155.  
  156. API_EXPORT(void) ap_cleanup_for_exec(void);
  157.  
  158. /* routines to allocate memory from an pool... */
  159.  
  160. API_EXPORT(void *) ap_palloc(struct pool *, int nbytes);
  161. API_EXPORT(void *) ap_pcalloc(struct pool *, int nbytes);
  162. API_EXPORT(char *) ap_pstrdup(struct pool *, const char *s);
  163. /* make a nul terminated copy of the n characters starting with s */
  164. API_EXPORT(char *) ap_pstrndup(struct pool *, const char *s, int n);
  165. API_EXPORT_NONSTD(char *) ap_pvstrcat(struct pool *, va_list);
  166. API_EXPORT_NONSTD(char *) ap_pstrcat(struct pool *,...);    /* all '...' must be char* */
  167. API_EXPORT_NONSTD(char *) ap_psprintf(struct pool *, const char *fmt, ...)
  168.     __attribute__((format(printf,2,3)));
  169. API_EXPORT(char *) ap_pvsprintf(struct pool *, const char *fmt, va_list);
  170.  
  171. /* array and alist management... keeping lists of things.
  172.  * Common enough to want common support code ...
  173.  */
  174.  
  175. typedef struct {
  176.     ap_pool *pool;
  177.     int elt_size;
  178.     int nelts;
  179.     int nalloc;
  180.     char *elts;
  181. } array_header;
  182.  
  183. API_EXPORT(array_header *) ap_make_array(pool *p, int nelts, int elt_size);
  184. API_EXPORT(void *) ap_push_array(array_header *);
  185. API_EXPORT(void) ap_array_cat(array_header *dst, const array_header *src);
  186. API_EXPORT(array_header *) ap_append_arrays(pool *, const array_header *,
  187.                      const array_header *);
  188.  
  189. /* ap_array_pstrcat generates a new string from the pool containing
  190.  * the concatenated sequence of substrings referenced as elements within
  191.  * the array.  The string will be empty if all substrings are empty or null,
  192.  * or if there are no elements in the array.
  193.  * If sep is non-NUL, it will be inserted between elements as a separator.
  194.  */
  195. API_EXPORT(char *) ap_array_pstrcat(pool *p, const array_header *arr,
  196.                                     const char sep);
  197.  
  198. /* copy_array copies the *entire* array.  copy_array_hdr just copies
  199.  * the header, and arranges for the elements to be copied if (and only
  200.  * if) the code subsequently does a push or arraycat.
  201.  */
  202.  
  203. API_EXPORT(array_header *) ap_copy_array(pool *p, const array_header *src);
  204. API_EXPORT(array_header *) ap_copy_array_hdr(pool *p, const array_header *src);
  205.  
  206.  
  207. /* Tables.  Implemented alist style, for now, though we try to keep
  208.  * it so that imposing a hash table structure on top in the future
  209.  * wouldn't be *too* hard...
  210.  *
  211.  * Note that key comparisons for these are case-insensitive, largely
  212.  * because that's what's appropriate and convenient everywhere they're
  213.  * currently being used...
  214.  */
  215.  
  216. typedef struct table table;
  217.  
  218. typedef struct {
  219.     char *key;        /* maybe NULL in future;
  220.              * check when iterating thru table_elts
  221.              */
  222.     char *val;
  223. } table_entry;
  224.  
  225. API_EXPORT(table *) ap_make_table(pool *p, int nelts);
  226. API_EXPORT(table *) ap_copy_table(pool *p, const table *);
  227. API_EXPORT(void) ap_clear_table(table *);
  228. API_EXPORT(const char *) ap_table_get(const table *, const char *);
  229. API_EXPORT(void) ap_table_set(table *, const char *name, const char *val);
  230. API_EXPORT(void) ap_table_setn(table *, const char *name, const char *val);
  231. API_EXPORT(void) ap_table_merge(table *, const char *name, const char *more_val);
  232. API_EXPORT(void) ap_table_mergen(table *, const char *name, const char *more_val);
  233. API_EXPORT(void) ap_table_unset(table *, const char *key);
  234. API_EXPORT(void) ap_table_add(table *, const char *name, const char *val);
  235. API_EXPORT(void) ap_table_addn(table *, const char *name, const char *val);
  236. API_EXPORT(void) ap_table_do(int (*comp) (void *, const char *, const char *), void *rec,
  237.               const table *t,...);
  238.  
  239. API_EXPORT(table *) ap_overlay_tables(pool *p, const table *overlay, const table *base);
  240.  
  241. /* Conceptually, ap_overlap_tables does this:
  242.  
  243.     array_header *barr = ap_table_elts(b);
  244.     table_entry *belt = (table_entry *)barr->elts;
  245.     int i;
  246.  
  247.     for (i = 0; i < barr->nelts; ++i) {
  248.     if (flags & AP_OVERLAP_TABLES_MERGE) {
  249.         ap_table_mergen(a, belt[i].key, belt[i].val);
  250.     }
  251.     else {
  252.         ap_table_setn(a, belt[i].key, belt[i].val);
  253.     }
  254.     }
  255.  
  256.     Except that it is more efficient (less space and cpu-time) especially
  257.     when b has many elements.
  258.  
  259.     Notice the assumptions on the keys and values in b -- they must be
  260.     in an ancestor of a's pool.  In practice b and a are usually from
  261.     the same pool.
  262. */
  263. #define AP_OVERLAP_TABLES_SET    (0)
  264. #define AP_OVERLAP_TABLES_MERGE    (1)
  265. API_EXPORT(void) ap_overlap_tables(table *a, const table *b, unsigned flags);
  266.  
  267. /* XXX: these know about the definition of struct table in alloc.c.  That
  268.  * definition is not here because it is supposed to be private, and by not
  269.  * placing it here we are able to get compile-time diagnostics from modules
  270.  * written which assume that a table is the same as an array_header. -djg
  271.  */
  272. #define ap_table_elts(t) ((array_header *)(t))
  273. #define ap_is_empty_table(t) (((t) == NULL)||(((array_header *)(t))->nelts == 0))
  274.  
  275. /* routines to remember allocation of other sorts of things...
  276.  * generic interface first.  Note that we want to have two separate
  277.  * cleanup functions in the general case, one for exec() preparation,
  278.  * to keep CGI scripts and the like from inheriting access to things
  279.  * they shouldn't be able to touch, and one for actually cleaning up,
  280.  * when the actual server process wants to get rid of the thing,
  281.  * whatever it is.  
  282.  *
  283.  * kill_cleanup disarms a cleanup, presumably because the resource in
  284.  * question has been closed, freed, or whatever, and it's scarce
  285.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  286.  * resource not to be cleaned up a second time (it might have been
  287.  * reallocated).  run_cleanup does the same, but runs it first.
  288.  *
  289.  * Cleanups are identified for purposes of finding & running them off by the
  290.  * plain_cleanup and data, which should presumably be unique.
  291.  *
  292.  * NB any code which invokes register_cleanup or kill_cleanup directly
  293.  * is a critical section which should be guarded by block_alarms() and
  294.  * unblock_alarms() below...
  295.  */
  296.  
  297. API_EXPORT(void) ap_register_cleanup(pool *p, void *data,
  298.                   void (*plain_cleanup) (void *),
  299.                   void (*child_cleanup) (void *));
  300.  
  301. API_EXPORT(void) ap_kill_cleanup(pool *p, void *data, void (*plain_cleanup) (void *));
  302. API_EXPORT(void) ap_run_cleanup(pool *p, void *data, void (*cleanup) (void *));
  303.  
  304. /* A "do-nothing" cleanup, for register_cleanup; it's faster to do
  305.  * things this way than to test for NULL. */
  306. API_EXPORT_NONSTD(void) ap_null_cleanup(void *data);
  307.  
  308. /* The time between when a resource is actually allocated, and when it
  309.  * its cleanup is registered is a critical section, during which the
  310.  * resource could leak if we got interrupted or timed out.  So, anything
  311.  * which registers cleanups should bracket resource allocation and the
  312.  * cleanup registry with these.  (This is done internally by run_cleanup).
  313.  *
  314.  * NB they are actually implemented in http_main.c, since they are bound
  315.  * up with timeout handling in general...
  316.  */
  317.  
  318. #ifdef TPF
  319. #define ap_block_alarms() (0)
  320. #define ap_unblock_alarms() (0)
  321. #else
  322. API_EXPORT(void) ap_block_alarms(void);
  323. API_EXPORT(void) ap_unblock_alarms(void);
  324. #endif /* TPF */
  325.  
  326. /* Common cases which want utility support..
  327.  * the note_cleanups_for_foo routines are for 
  328.  */
  329.  
  330. API_EXPORT(FILE *) ap_pfopen(struct pool *, const char *name, const char *fmode);
  331. API_EXPORT(FILE *) ap_pfdopen(struct pool *, int fd, const char *fmode);
  332. API_EXPORT(int) ap_popenf(struct pool *, const char *name, int flg, int mode);
  333.  
  334. API_EXPORT(void) ap_note_cleanups_for_file(pool *, FILE *);
  335. API_EXPORT(void) ap_note_cleanups_for_fd(pool *, int);
  336. #ifdef WIN32
  337. API_EXPORT(void) ap_note_cleanups_for_h(pool *, HANDLE);
  338. #endif
  339. API_EXPORT(void) ap_kill_cleanups_for_fd(pool *p, int fd);
  340.  
  341. API_EXPORT(void) ap_note_cleanups_for_socket(pool *, int);
  342. API_EXPORT(void) ap_kill_cleanups_for_socket(pool *p, int sock);
  343. API_EXPORT(int) ap_psocket(pool *p, int, int, int);
  344. API_EXPORT(int) ap_pclosesocket(pool *a, int sock);
  345.  
  346. API_EXPORT(regex_t *) ap_pregcomp(pool *p, const char *pattern, int cflags);
  347. API_EXPORT(void) ap_pregfree(pool *p, regex_t * reg);
  348.  
  349. /* routines to note closes... file descriptors are constrained enough
  350.  * on some systems that we want to support this.
  351.  */
  352.  
  353. API_EXPORT(int) ap_pfclose(struct pool *, FILE *);
  354. API_EXPORT(int) ap_pclosef(struct pool *, int fd);
  355. #ifdef WIN32
  356. API_EXPORT(int) ap_pcloseh(struct pool *, HANDLE hDevice);
  357. #endif
  358.  
  359. /* routines to deal with directories */
  360. API_EXPORT(DIR *) ap_popendir(pool *p, const char *name);
  361. API_EXPORT(void) ap_pclosedir(pool *p, DIR * d);
  362.  
  363. /* ... even child processes (which we may want to wait for,
  364.  * or to kill outright, on unexpected termination).
  365.  *
  366.  * ap_spawn_child is a utility routine which handles an awful lot of
  367.  * the rigamarole associated with spawning a child --- it arranges
  368.  * for pipes to the child's stdin and stdout, if desired (if not,
  369.  * set the associated args to NULL).  It takes as args a function
  370.  * to call in the child, and an argument to be passed to the function.
  371.  */
  372.  
  373. enum kill_conditions {
  374.     kill_never,            /* process is never sent any signals */
  375.     kill_always,        /* process is sent SIGKILL on pool cleanup */
  376.     kill_after_timeout,        /* SIGTERM, wait 3 seconds, SIGKILL */
  377.     just_wait,            /* wait forever for the process to complete */
  378.     kill_only_once        /* send SIGTERM and then wait */
  379. };
  380.  
  381. typedef struct child_info child_info;
  382. API_EXPORT(void) ap_note_subprocess(pool *a, pid_t pid,
  383.                     enum kill_conditions how);
  384. API_EXPORT(int) ap_spawn_child(pool *, int (*)(void *, child_info *),
  385.                    void *, enum kill_conditions,
  386.                    FILE **pipe_in, FILE **pipe_out,
  387.                    FILE **pipe_err);
  388.  
  389. /* magic numbers --- min free bytes to consider a free pool block useable,
  390.  * and the min amount to allocate if we have to go to malloc() */
  391.  
  392. #ifndef BLOCK_MINFREE
  393. #define BLOCK_MINFREE 4096
  394. #endif
  395. #ifndef BLOCK_MINALLOC
  396. #define BLOCK_MINALLOC 8192
  397. #endif
  398.  
  399. /* Finally, some accounting */
  400.  
  401. API_EXPORT(long) ap_bytes_in_pool(pool *p);
  402. API_EXPORT(long) ap_bytes_in_free_blocks(void);
  403.  
  404. #ifdef __cplusplus
  405. }
  406. #endif
  407.  
  408. #endif    /* !APACHE_ALLOC_H */
  409.